package xyz.gits.modules.weread.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import xyz.gits.modules.base.GitsBaseController;
import xyz.gits.modules.weread.entity.UnLimit;
import xyz.gits.modules.weread.service.IUnLimitService;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 无限卡 前端控制器
 * </p>
 *
 * @author 读钓
 * @date 2019-05-12
 */
@Controller
@RequestMapping("/weread/unLimit")
public class UnLimitController extends GitsBaseController {

    private String prefix = "/weread/unLimit";

    @Autowired
    private IUnLimitService unLimitService;

    @RequiresPermissions("weread:unLimit:view")
    @GetMapping()
    public String unLimit() {
        return prefix + "/unLimit";
    }

    @ApiOperation("查询无限卡列表")
    @RequiresPermissions("weread:unLimit:list")
    @PostMapping(value = "/list")
    @ResponseBody
    public TableDataInfo list(UnLimit unLimit) {
        QueryWrapper<UnLimit> qw = new QueryWrapper(unLimit);
        Page page = getPage(qw);
        IPage result = unLimitService.page(page, qw);
        return getDataTable(result);
    }

    @ApiOperation("导出无限卡列表")
    @RequiresPermissions("weread:unLimit:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UnLimit unLimit) {
        List<UnLimit> list = unLimitService.list(new QueryWrapper<>(unLimit));
        ExcelUtil<UnLimit> util = new ExcelUtil<UnLimit>(UnLimit.class);
        return util.exportExcel(list, "unLimit");
    }

    @ApiOperation("新增无限卡")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    @ApiOperation("新增保存无限卡")
    @RequiresPermissions("weread:unLimit:add")
    @Log(title = "无限卡", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UnLimit unLimit) {
        return toAjax(unLimitService.save(unLimit));
    }

    @ApiOperation("修改无限卡")
    @GetMapping("/edit/{unLimitId}")
    public String edit(@PathVariable("unLimitId") String unLimitId, ModelMap mmap) {
        UnLimit unLimit = unLimitService.getById(unLimitId);
        mmap.put("unLimit", unLimit);
        return prefix + "/edit";
    }

    @ApiOperation("修改保存无限卡")
    @RequiresPermissions("weread:unLimit:edit")
    @Log(title = "无限卡", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UnLimit UnLimit) {
        return toAjax(unLimitService.updateById(UnLimit));
    }

    @ApiOperation("删除无限卡")
    @RequiresPermissions("weread:unLimit:remove")
    @Log(title = "无限卡", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(unLimitService.removeByIds(Arrays.asList(ids.split(","))));
    }

}
